package breakthrough;

import game.GameMove;
import game.GameState;
import game.Util;

public class BetterBreakthroughState extends BreakthroughState{

	public BreakthroughMove lastMove;
	
	public BetterBreakthroughState(BreakthroughState state){
		
		Util.copy(this.board, state.board);
		this.who = state.who;
		this.numMoves = state.numMoves;
		this.status = state.status;
	}
	
	public BetterBreakthroughState(GameState state){
		this.who = state.who;
		this.numMoves = state.numMoves;
		this.status = state.status;
	}
	
	/**
	 * For cloning
	 */
	public BetterBreakthroughState(BetterBreakthroughState state){
		this.lastMove = state.lastMove;
		Util.copy(this.board, state.board);
		this.who = state.who;
		this.numMoves = state.numMoves;
		this.status = state.status;
	}
	
	/**
	 * Extends the functionality of makeMove to also save the most recent move that was 
	 * made so we can have an undoLastMove() method.
	 */
	public boolean makeMove(GameMove m){
		
		if(super.makeMove(m)){
			this.lastMove = (BreakthroughMove)m;
			return true;
		}
		
		return false;
	}
	
	/**
	 * Undoes the last move that has been made. This allows us to save time cloning 
	 * states.
	 */
	public void undoLastMove(){
		
		char PLAYER = (who == GameState.Who.HOME ? homeSym : awaySym);
		
		this.board[this.lastMove.endingRow][this.lastMove.endingCol] = emptySym;
		this.board[this.lastMove.startRow][this.lastMove.startCol] = PLAYER;
		
		this.numMoves--;
		togglePlayer();
		this.status = GameState.Status.GAME_ON;
	}
	
	public Object clone()
	{
		BetterBreakthroughState res = new BetterBreakthroughState(this);
		return res;
	}
}
